package com.nimt.hqgl_idea.mapper;


import com.nimt.hqgl_idea.entity.GoodsUseRecord;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "goodsUseRecordMapper")
public interface GoodsUseRecordMapper {
    @Select("<script>" +
            "select a.*,b.userName,c.name as goodsName from goods_use_record a inner join user b on a.userId = b.id inner join goods c on a.goodsId = c.id" +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='userId!=null'> and userId = #{userId}</if>" +
            "<if test='keywords!=null'> and goodsId = #{keywords} or userId = #{keywords}</if>" +
            "</trim>" +
            " limit #{start},#{pageSize}" +
            "</script>")
    List<GoodsUseRecord> selectAll(@Param("start") Integer start,
                                   @Param("pageSize") Integer pageSize,
                                   @Param("userId") Integer userId,
                                   @Param("keywords") String keywords);

    @Select("<script>" +
            "select count(1) from `goods_use_record` " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='userId!=null'> and userId = #{userId}</if>" +
            "<if test='keywords!=null'> and goodsId = #{keywords} or userId = #{keywords}</if>" +
            "</trim>" +
            "</script>")
    int count(Integer userId,
              @Param("keywords") String keywords);

    @Delete("delete from `goods_use_record` where id = #{id}")
    int delete(Integer id);
}
